package org.szgb.console.sys.custom.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class SysCustomFormRecordItem extends BeanEntity<SysCustomFormRecordItem> {
    @Serial
    private static final long serialVersionUID = 1L;
    private SysCustomFormRecord record;
    private Integer type;
    private String label;
    private String value;
    private String options;
    private Boolean required;
    private Integer sort;


    public SysCustomFormRecordItem() {
    }

    public SysCustomFormRecordItem(String id) {
        this.setId(id);
    }

    public SysCustomFormRecordItem(SysCustomFormRecordItem.SysCustomFormRecordItemFindDto SysCustomFormRecordItemFindDto){
        this.setId(SysCustomFormRecordItemFindDto.getId());
        if(SysCustomFormRecordItemFindDto.getRecordId() != null){
            this.record = new SysCustomFormRecord(SysCustomFormRecordItemFindDto.getRecordId());
        }
        this.type = SysCustomFormRecordItemFindDto.getType();
        this.label = SysCustomFormRecordItemFindDto.getLabel();
        this.options = SysCustomFormRecordItemFindDto.getOptions();
        this.required = SysCustomFormRecordItemFindDto.getRequired();
        this.sort = SysCustomFormRecordItemFindDto.getSort();
        this.setPageDto(SysCustomFormRecordItemFindDto.getPageDto());
    }

    public SysCustomFormRecordItem(SysCustomFormRecordItem.SysCustomFormRecordItemSaveDto SysCustomFormRecordItemSaveDto){
        this.setId(SysCustomFormRecordItemSaveDto.id);
        if(SysCustomFormRecordItemSaveDto.recordId != null){
            this.record = new SysCustomFormRecord(SysCustomFormRecordItemSaveDto.recordId);
        }
        this.type = SysCustomFormRecordItemSaveDto.type;
        this.label = SysCustomFormRecordItemSaveDto.label;
        this.options = SysCustomFormRecordItemSaveDto.options;
        this.required = SysCustomFormRecordItemSaveDto.required;
        this.sort = SysCustomFormRecordItemSaveDto.sort;
    }

    public SysCustomFormRecordItem.SysCustomFormRecordItemView toSysCustomFormRecordItemView() {
        return new SysCustomFormRecordItem.SysCustomFormRecordItemView(this);
    }

    @Data
    @Schema(title = "自定义表单记录子项信息", description = "自定义表单记录子项信息")
    public static class SysCustomFormRecordItemView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "记录id")
        private String recordId;
        @Schema(description = "类型（字典）")
        private Integer type;
        @Schema(description = "标题")
        private String label;
        @Schema(description = "选项（多个逗号隔开）")
        private String options;
        @Schema(description = "值（多个逗号隔开）")
        private String value;
        @Schema(description = "是否必填")
        private Boolean required;
        @Schema(description = "排序")
        private Integer sort;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public SysCustomFormRecordItemView(SysCustomFormRecordItem SysCustomFormRecordItem) {
            this.id = SysCustomFormRecordItem.getId();
            if(SysCustomFormRecordItem.getRecord() != null){
                this.recordId = SysCustomFormRecordItem.getRecord().getId();
            }
            this.type = SysCustomFormRecordItem.getType();
            this.label = SysCustomFormRecordItem.getLabel();
            this.options = SysCustomFormRecordItem.getOptions();
            this.required = SysCustomFormRecordItem.getRequired();
            this.sort = SysCustomFormRecordItem.getSort();
            this.createdAt = SysCustomFormRecordItem.getCreatedAt();
            this.updatedAt = SysCustomFormRecordItem.getUpdatedAt();
            if (SysCustomFormRecordItem.getCreatedBy() != null) {
                this.createdById = SysCustomFormRecordItem.getCreatedBy().getId();
                this.createdByName = SysCustomFormRecordItem.getCreatedBy().getName();
            }
            if (SysCustomFormRecordItem.getUpdatedBy() != null) {
                this.updatedById = SysCustomFormRecordItem.getUpdatedBy().getId();
                this.updatedByName = SysCustomFormRecordItem.getUpdatedBy().getName();
            }
        }

        public static List<SysCustomFormRecordItem.SysCustomFormRecordItemView> getViewListFromList(List<SysCustomFormRecordItem> SysCustomFormRecordItemList) {
            return SysCustomFormRecordItemList.stream().map(SysCustomFormRecordItem.SysCustomFormRecordItemView::new).toList();
        }
    }

    @Data
    @Schema(title = "自定义表单记录子项查询表单", description = "自定义表单记录子项查询参数")
    public static class SysCustomFormRecordItemFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "记录id")
        private String recordId;
        @Schema(description = "类型（字典）")
        private Integer type;
        @Schema(description = "标题")
        private String label;
        @Schema(description = "值（多个逗号隔开）")
        private String value;
        @Schema(description = "选项（多个逗号隔开）")
        private String options;
        @Schema(description = "是否必填")
        private Boolean required;
        @Schema(description = "排序")
        private Integer sort;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public SysCustomFormRecordItem toSysCustomFormRecordItem() {
            return new SysCustomFormRecordItem(this);
        }
    }

    @Data
    @Schema(title = "自定义表单记录子项保存表单", description = "自定义表单记录子项保存参数")
    public static class SysCustomFormRecordItemSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "记录id")
        private String recordId;
        @Schema(description = "类型（字典）")
        private Integer type;
        @Schema(description = "标题")
        private String label;
        @Schema(description = "值（多个逗号隔开）")
        private String value;
        @Schema(description = "选项（多个逗号隔开）")
        private String options;
        @Schema(description = "是否必填")
        private Boolean required;
        @Schema(description = "排序")
        private Integer sort;

        public SysCustomFormRecordItem toSysCustomFormRecordItem() {
            return new SysCustomFormRecordItem(this);
        }
    }
}
